package com.ruoyi.game.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.game.domain.entity.GamePick3Record;
import com.ruoyi.game.domain.entity.GameWingoRecord;
import com.ruoyi.game.mapper.GameWingoRecordMapper;
import com.ruoyi.game.service.IGameWingoRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * wingo开奖记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-13
 */
@Service
public class GameWingoRecordServiceImpl implements IGameWingoRecordService
{
    @Resource
    private GameWingoRecordMapper gameWingoRecordMapper;


@Override
public List<GameWingoRecord> page(int pageNo, int pageSize,int timeLimit ) {

    PageHelper.startPage(pageNo,pageSize);
    PageHelper.orderBy("round_no desc");

    PageInfo pageInfo = new PageInfo();
    int totalPages = pageInfo.getPages();  //总页数
    long totalRecords = pageInfo.getTotal(); //总记录数
    boolean isFirstPage = pageInfo.isIsFirstPage(); //是否为第一页
    boolean isLastPage = pageInfo.isIsLastPage(); //是否为最后一页
//    System.out.println("----总页数---"+totalPages+"---总记录数---"+totalRecords+"是否为第一页"+isFirstPage+"是否为最后一页"+isLastPage);
    return gameWingoRecordMapper.page(timeLimit);
}
    @Override
    public GameWingoRecord findByRoundNo(String roundNo) {
        GameWingoRecord gameWingoRecord = new GameWingoRecord();
        gameWingoRecord.setRoundNo(roundNo);

        List<GameWingoRecord> list = gameWingoRecordMapper.selectGameWingoRecordList(gameWingoRecord);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    @Override
    public boolean updateLottery(String roundNo,  String[] ballNo) {
    //根据场次查询是否有记录
        GameWingoRecord gameWingoRecord = findByRoundNo(roundNo);
        System.out.println("*11*");
        if(ObjectUtil.isNull(gameWingoRecord)){
            System.out.println("*22*");
            return false;
        }
        //判断当前场次是否已经结束
        if(gameWingoRecord.getToEnd() == Constants.NUM_ONE){
            System.out.println("*33*");
            return false;
        }
        System.out.println("*44*");
        GameWingoRecord update = new GameWingoRecord();
        update.setId(gameWingoRecord.getId());
        update.setFirstNo(ballNo[0]);
        update.setSecondNo(ballNo[1]);
        update.setThirdNo(ballNo[2]);
        update.setToEnd(Constants.NUM_ONE);
        return gameWingoRecordMapper.updateGameWingoRecord(update) > 0;
    }

    @Override
    public void saveBatch(List<GameWingoRecord> list) {
        String roundNo = list.get(0).getRoundNo();
        GameWingoRecord gameWingoRecord = findByRoundNo(roundNo);
        if(ObjectUtil.isNotNull(gameWingoRecord)){
            return;
        }
        for(GameWingoRecord record : list){
            System.out.println("插入开奖记录");
            gameWingoRecordMapper.insertGameWingoRecord(record);
        }
    }
}
